<?php

/**
 * This is the model class for table "clases".
 *
 * The followings are the available columns in table 'clases':
 * @property integer $id_clase
 * @property integer $id_horario
 * @property integer $id
 * @property string $fecha
 * @property string $hora_inicio
 * @property string $hora_fin
 * @property string $nombre
 * @property string $descripcion
 *
 * The followings are the available model relations:
 * @property Marcaciones[] $marcaciones
 * @property Horarios $idHorario
 * @property Profesores $id0
 * @property Clasesrecuperacion $clasesrecuperacion
 * @property Clasesrecuperacion[] $clasesrecuperacions
 * @property Solicitudescambiofecha[] $solicitudescambiofechas
 */
class Clases extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'clases';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id, fecha, nombre, descripcion', 'required',"message"=>"No se puede dejar en blanco {attribute}"),
			array('id_horario, id', 'numerical', 'integerOnly'=>true,"message"=>"Tiene que ser un ID valido"),
			array('nombre', 'length', 'max'=>100),
			array('hora_inicio, hora_fin', 'date','format'=>'HH:mm:ss','message'=>'Error al cargar {attribute} - El formato requerido es  HH:mm:ss'),
			//array('hora_fin', 'compare', 'compareAttribute'=>'hora_inicio','operator'=>'>'),
			
			
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			//array('id_clase, id_horario, id, fecha, hora_inicio, hora_fin, nombre, descripcion', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'marcaciones' => array(self::MANY_MANY, 'Marcaciones', 'marcaciones_clases(id_clase, id_marcacion)'),
			'idHorario' => array(self::BELONGS_TO, 'Horarios', 'id_horario'),
			'id0' => array(self::BELONGS_TO, 'Profesores', 'id'),
			'clasesrecuperacion' => array(self::HAS_ONE, 'Clasesrecuperacion', 'clase_original'),
			'clasesrecuperacions' => array(self::HAS_MANY, 'Clasesrecuperacion', 'clase_recuperacion'),
			'solicitudescambiofechas' => array(self::MANY_MANY, 'Solicitudescambiofecha', 'cursos_solicitudescambio(id_clase, id_solicitud)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_clase' => 'Clase',
			'id_horario' => 'Horario',
			'id' => 'Profesor',
			'fecha' => 'Fecha',
			'hora_inicio' => 'Hora de inicio',
			'hora_fin' => 'Hora de fin',
			'nombre' => 'Nombre',
			'descripcion' => 'Descripción',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_clase',$this->id_clase);
		$criteria->compare('id_horario',$this->id_horario);
		$criteria->compare('id',$this->id);
		$criteria->compare('fecha',$this->fecha,true);
		$criteria->compare('hora_inicio',$this->hora_inicio,true);
		$criteria->compare('hora_fin',$this->hora_fin,true);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('descripcion',$this->descripcion,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Clases the static model class
	 */
	
	function getDisplay() // Función en que concatena la fecha inicial y final del periodo lectivo
	{

	    return $this->fecha.' - '.$this->hora_inicio.' - '.$this->nombre;
	}

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
